<?php


namespace App\Http\Repositories\ProductCenter;


use App\Http\Repositories\BaseRepository;
use App\Models\ProductCenter\Models\CategoryModel;

class CategoryRepository extends BaseRepository
{
    public $category;
    public function __construct(CategoryModel $category)
    {
        $this->category = $category;
    }

    public function getCategoryInfo($data)
    {
        $where['id'] = $data['id'];
        return $this->category->getInfo($where);
    }

    public function getCategoryList($data)
    {
        $where = [];
        is_real_exists(@$data['name']) && $where['name'] = ['like', '%'.$data['name'].'%'];
        is_real_exists(@$data['status']) && $where['status'] = $data['status'];

        switch ($data['operate']) {
            case 1:
                return $this->category->getListByPage($where);
            case 2:
                return $this->category->getList($where);
            case 3:
                $field = 'id,id as value,parent_id,name,name as label';
                $category = $this->category->getList([], $field)->toArray();
                return $this->generateTree($category);
            default:
                return [];
        }


    }

    public function addCategory($data)
    {
        return $this->category->addItem($data, true);
    }

    public function editCategory($data)
    {
        $where['id'] = $data['id'];

        $this->category->editItem($where, $data);

        return [];
    }

    public function delCategory($data)
    {
        return $this->category->deleteItem($data);
    }

    public function getCategoryDownList($data)
    {
        $where['status'] = $this->category::TRUE;
        $res = $this->category
            ->handleCondition($where)
            ->orderBy('sort', 'desc')
            ->selectRaw('id,id as value,parent_id,name,name as label')
            ->get()
            ->toArray();

        return $this->generateTreeStr($res);
    }
}
